package com.yaozheng.vocationaltraining.fragment.exam;

import android.view.View;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.activity.HomeActivity;
import com.yaozheng.vocationaltraining.adapter.PKCategoryAdapter;
import com.yaozheng.vocationaltraining.adapter.PKRankAdapter;
import com.yaozheng.vocationaltraining.bean.PKCategoryBean;
import com.yaozheng.vocationaltraining.bean.PKRankBean;
import com.yaozheng.vocationaltraining.bean.WinRateBean;
import com.yaozheng.vocationaltraining.fragment.BaseFragment;
import com.yaozheng.vocationaltraining.iview.IPKRankView;
import com.yaozheng.vocationaltraining.iview.exam.IPKView;
import com.yaozheng.vocationaltraining.service.exam.PKRankService;
import com.yaozheng.vocationaltraining.service.exam.PKService;
import com.yaozheng.vocationaltraining.service.impl.exam.PKRankServiceImpl;
import com.yaozheng.vocationaltraining.service.impl.exam.PKServiceImpl;
import com.yaozheng.vocationaltraining.utils.BitmapLoader;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;
import com.yaozheng.vocationaltraining.widget.CircleImageView;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by moon on 2017/11/14.
 */

@EFragment(R.layout.activity_pk_rank)
public class PKRankFragment extends BaseFragment implements IPKRankView {
    @ViewById
    ListView listView;

    PKRankAdapter pkRankAdapter;
    ArrayList<PKRankBean> pkRankData = new ArrayList<>();

    @ViewById
    TextView headTitleText;
    @ViewById
    TextView jifeng_number_textview;
    @ViewById
    CircleImageView userAvatarImage;
    @ViewById
    TextView username_textview;
    @ViewById
    TextView winning_textview;

    @ViewById
    TextView my_rank_textview;

    @Bean(PKRankServiceImpl.class)
    PKRankService pkRankService;

    private BitmapLoader userBitmapLoader;

    public HomeActivity getHomeActivity() {
        return (HomeActivity) getActivity();
    }

    int jifengNumber;
    String name;
    String avatarUrl;
    int winRate;

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    int userId;

    public int getJifengNumber() {
        return jifengNumber;
    }

    public void setJifengNumber(int jifengNumber) {
        this.jifengNumber = jifengNumber;

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;

    }

    public String getAvatarUrl() {
        return avatarUrl;
    }

    public void setAvatarUrl(String avatarUrl) {
        this.avatarUrl = avatarUrl;

    }

    public int getWinRate() {
        return winRate;
    }

    public void setWinRate(int winRate) {
        this.winRate = winRate;

    }

    @AfterViews
    public void initView() {
        headTitleText.setText("排行榜");
        pkRankService.init(this);
        userBitmapLoader = new BitmapLoader(Glide.with(this), R.drawable.user_img);

        pkRankAdapter = new PKRankAdapter(this.getActivity(),pkRankData);
        listView.setAdapter(pkRankAdapter);

        username_textview.setText(name);
        jifeng_number_textview.setText(jifengNumber+"");
        //加载图片
        userBitmapLoader.displayImage(avatarUrl, userAvatarImage);
        if (winRate != 0){
            winning_textview.setText(winRate/ 100.0+"");
        }else {
            winning_textview.setText("--");
        }

        loadMyRank();

    }

    private void loadMyRank() {
        pkRankService.loadMyRank();
    }

    private void onRefresh() {
        pkRankService.list();
    }


    @UiThread
    @Override
    public void responseSuccess(JSONObject jsonObject) {
        try {
            cancelLoadDataProgressDialog();
            JSONArray dataList = TypeUtils.getJsonArray(jsonObject, "data","rank");
            if (dataList != null) {
                pkRankData.clear();

                for (int i = 0; i < dataList.size(); i++) {
                    JSONObject jsonObject2 = dataList.getJSONObject(i);

                    PKRankBean bean = new PKRankBean();
                    bean.setId(jsonObject2.getInt("id"));
                    bean.setName(jsonObject2.getString("name"));
                    bean.setUid(jsonObject2.getInt("uid"));
                    bean.setRate(jsonObject2.getInt("rate"));
                    bean.setUpdatedTime(jsonObject2.getString("updatedTime"));
                    bean.setUsername(jsonObject2.getString("username"));
                    bean.setDepartment(jsonObject2.getString("department"));
                    bean.setPosition(jsonObject2.getString("position"));
                    bean.setStoreId(jsonObject2.getInt("storeId"));
                    bean.setAvatar(jsonObject2.getString("avatar"));
                    bean.setStoreName(jsonObject2.getString("storeName"));

                    pkRankData.add(bean);
                }

                pkRankAdapter.setData(pkRankData,userId);
                pkRankAdapter.notifyDataSetChanged();
            }
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
        }
    }

    @UiThread
    @Override
    public void responseError(String error) {

    }

    @UiThread
    @Override
    public void myRankSuccess(JSONObject jsonObject) {
        try {
            cancelLoadDataProgressDialog();
            JSONObject myRankObject = TypeUtils.getJsonObject(jsonObject, "data","rank");
            if (myRankObject != null && myRankObject.containsKey("rank")) {

                my_rank_textview.setText(myRankObject.getInt("rank")+"");

            }else {
                my_rank_textview.setText("--");
            }
            userId = myRankObject.getInt("userId");
            onRefresh();
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            my_rank_textview.setText("--");
        }
    }

    @UiThread
    @Override
    public void myRankError(String error) {

    }
}
